var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    times: ['累计', '今日', '本周', '本月', '本年'],
    page: 1,
    limit: 100,
    currentTab: 0,
    total_page: 0,
    saleltList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 隐藏分享
    wx.hideShareMenu()
  },

  onShow:function(){
    this.setData({
      page:1,
      currentTab:0
    })
    this.saleListGet();
    this.saleStatistics();
  },


  //销量列表
  saleListGet:function(){
    var that = this;
    var page = this.data.page;
    var time = this.data.currentTab;
    var limit = this.data.limit;
    var params = {
      limit: that.data.limit,
      time: time,
      page: page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'inn/saleDetail',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var saleltList = res.data.data.list;
          if (page == 1) {
            var itemData = saleltList;
          } else {
            var itemData = that.data.saleltList.concat(saleltList)
          }

          that.setData({
            total_page: totalPage,
            countAll: paging.count,
            saleltList: itemData
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");

      }
    })
  },

  //销量统计
  saleStatistics:function(){
    var that = this

    // var time = this.data.currentTab;
    api.POST({
      params: { time:0},
      url: app.globalData.base_url + 'inn/saleStatistics',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            saleAll: res.data.data.all
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");

      }
    })

  },


  swichNav: function (e) {
    this.setData({
      currentTab: e.target.dataset.currentTime,
      page: 1
    })

    this.saleListGet();
    this.saleStatistics();
  },


    onShareAppMessage: function (res) {
    var that = this;
    var nickname = wx.getStorageSync('accountInfo').nickname;
    var inn_id = wx.getStorageSync('accountInfo').inn_id;
      var url = '/agent/pages/storeGoodsList/storeGoodsList?inn_id=' + inn_id;
      var share_img = app.globalData.base_url + 'images/share_01.jpg'
    var title = nickname + '向你分享了ta的小店'
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: title,
        path: url,
        imageUrl: share_img,
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  },



  //页面下拉刷新
  onPullDownRefresh: function () {
    this.refresh();
  },

  //刷新数据
  refresh: function () {
    var that = this;
    that.setData({
      page: 1,
    });
    setTimeout(function () {
      that.saleListGet()
    }, 500)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  //加载更多
  loadMore: function () {
    var  that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      page: this.data.page + 1
    });
    setTimeout(function () {
      that.saleListGet()
    }, 500)
  },
})